//
//  StoryViewController.m
//  B
//
//  Created by Znt on 15/11/6.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "StoryViewController.h"
#import "ZNTCollectionViewCell.h"
#import "Model2.h"
#import "StotyDetainsViewController.h"
@interface StoryViewController ()<UICollectionViewDataSource ,UICollectionViewDelegate>

@property (nonatomic ,strong)UICollectionView *collectionView;
@property (nonatomic ,strong)NSMutableArray *allDataArray;
@property (nonatomic ,assign)NSInteger nextPage;
@property (nonatomic , assign)BOOL isUp ;

@end

@implementation StoryViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setAllDataArray:[NSMutableArray array]];
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10 ;
    flowLayout.itemSize = CGSizeMake(166 *XWIDTH , 170 *XHIGHT);
    [flowLayout setSectionInset:UIEdgeInsetsMake(10 *XHIGHT , 10 *XWIDTH, 10 *XHIGHT, 10 *XHIGHT)];
    
    [self setCollectionView:[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 375 *XWIDTH , 667 *XHIGHT- 64 - 49)  collectionViewLayout:flowLayout]  ];
    [[self collectionView]setBackgroundColor:[UIColor lightGrayColor]];
    [[self collectionView]setDelegate:self];
    [[self collectionView]setDataSource:self];
    [[self view]addSubview:[self collectionView]];
    
    [[self collectionView]registerClass:[ZNTCollectionViewCell class] forCellWithReuseIdentifier:@"aCell"];
    
    
    [self herderRefrash];
    [self footerRefresh];
    
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
}

- (void)herderRefrash {
    
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUp = NO;
        self.nextPage = 1;
        [self getData:self.nextPage];
        
    }];
    [self.collectionView.header beginRefreshing];
}

- (void)footerRefresh {
    
    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.isUp = YES;
        self.nextPage++;
        [self getData:self.nextPage];
        
    }];
    
    
}



-(void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:NO];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.allDataArray.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    ZNTCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"aCell" forIndexPath:indexPath];
    Model2 *mode = [self allDataArray][[indexPath item]];
    cell.model2 = mode;
    
    return cell;
    
}

- (void) getData:(NSInteger )nextPage {
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld",nextPage];
    
    
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [session GET:url_string parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *dic = responseObject;
        if (self.isUp == NO) {
            self.allDataArray = [NSMutableArray array];
        }
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        NSMutableArray *array = [dataDic objectForKey:@"hot_spot_list"];
        for (NSDictionary *dict in array) {
            
            NSDictionary *dicc = [dict objectForKey:@"user"];
            
            Model2 *mode2 = [[Model2 alloc]init];
            [mode2 setValuesForKeysWithDictionary:dict];
            mode2.autorName = [dicc objectForKey:@"name"];
            mode2.avatar_m = [dicc objectForKey:@"avatar_m"];
            [self.allDataArray addObject:mode2];
        }
        
        
        
        [[self collectionView]reloadData];
        [self.collectionView.header endRefreshing];
        [self.collectionView.footer endRefreshing];
        NSLog(@"成功");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [self.collectionView.header endRefreshing];
        [self.collectionView.footer endRefreshing];
        
        NSLog(@"失败");
    }];
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    Model2 *model  = [self allDataArray][[indexPath row]];
    
    StotyDetainsViewController *storDVC = [[StotyDetainsViewController alloc]init];
    [storDVC setModel2:model];
    [[self navigationController] pushViewController:storDVC animated:YES];
    
  
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
